package com.rr.learningdemo.thread;

/**
 * @author RR
 * @date 2021/3/15 22:28
 */
public class JoinTest implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new JoinTest());
        t.start();
        t.join(1000);
        synchronized (JoinTest.class){
            System.out.println("main fished");
        }
    }

    @Override
    public void run() {
        //这里加上锁，只有当下面都执行完，才会继续执行main下面的东西
        //不加锁t.join(1000)会在等待1秒后，就会继续执行下去，即 main fished和1 fished同时打印
//        synchronized (JoinTest.class){
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i + " fished");
            }
            System.out.println("test fished");
        }
//    }
}
